<?php

/**
 * 首页文件
 * ============================================================================
 * $Author: 王海龙 $
 * $Id: index.php 2015-01-01 王海龙 $
*/

define('IN_ECS', true);
require(dirname(__FILE__) . '/includes/init.php');
@ini_set('display_errors',        1);


/* AJAX获取品牌列表 */
if (!empty($_GET['act']) && $_GET['act'] == 'get_brand_list') {
    /* 随机获取品牌列表 */
    $brand_list = index_brand_list();
    $smarty->assign('brand_list',$brand_list);
    /* 获取品牌列表的HTML */
    $result['error'] = 0;
    $result['content'] = $smarty->fetch("index/brand.lbi");
    echo JSON()->encode($result);
    exit;
}
/* AJAX获取活动版块内容 */
if (!empty($_GET['act']) && $_GET['act'] == 'get_activity') {
    /* 活动广告图三个（竖） */
    $smarty->assign('ads_41', getAds(41));
    /* 活动广告图四个（横） */
    $smarty->assign('ads_42', getAds(42));
    /* 获取发现文章列表 */
    $smarty->assign('tvd_list', index_tvd_list());

    $result['error'] = 0;
    $result['content'] = $smarty->fetch("index/activity.lbi");
    echo JSON()->encode($result);
    exit;
}

/* AJAX获取专区商品列表内容 */
if (!empty($_GET['act']) && $_GET['act'] == 'get_area_goods') {
    if (!empty($_GET['cid']) && !empty($_GET['area'])) {
        $smarty->assign('goods_list', indexAreaGoods($_GET['area'], $_GET['cid']));
        $result['error'] = 0;
        $result['content'] = $smarty->fetch("index/goods.lbi");
        echo JSON()->encode($result);
        exit;
    }
    exit;
}

/* AJAX获取专区热门商品广告 */
if (!empty($_GET['act']) && $_GET['act'] == 'get_area_hot_ads') {
    if (!empty($_GET['area'])) {
        $smarty->assign('list', indexAreaHotAds($_GET['area']));
        $result['error'] = 0;
        $result['content'] = $smarty->fetch("index/hot_ads.lbi");
        echo JSON()->encode($result);
        exit;
    }
    exit;
}


    /* 初始化赋值模板 */
    assign_template();

    $smarty->assign('page_title',      htmlspecialchars($_CFG['shop_title'])); // 首页页面标题
    $smarty->assign('keywords',        htmlspecialchars($_CFG['shop_keywords'])); // 首页SEO网站关键词
    $smarty->assign('description',     htmlspecialchars($_CFG['shop_desc'])); // 首页SEO网站描述

    /* PC首页大轮播图*N */
    $smarty->assign('banner', getAds(47));

    /* PC首页新品嵌入广告位（大）*2 */
    $smarty->assign('ads_43', getAds(43));
    /* PC首页新品嵌入广告位（小）*2 */
    $smarty->assign('ads_44', getAds(44));

    /* PC首页横条广告位*3 */
    $smarty->assign('ads_45', getAds(45));


    /* 专区内容 */
    // 专区标识（书籍已隐藏'art_books'）
    $area       = array('arts_exam', 'children', 'acrylic_painting', 'design', 'rock_painting', );
    $adsArr     = getAds(46); // PC首页专区轮播广告位（6*3=18）个
    $cat2       = index_area_cat2(); // 二级分类
    $cat3       = index_area_cat3(); // 热门三级分类
    $brand_list = index_brand_list(); // 分类列表
    $hot_ads    = indexAreaHotAds(); // 专区热门商品广告
    /* 处理每个专区的数据 */
    foreach ($area as $key => $value) {
        $area_ads[$value]   = array_slice($adsArr, $key*3, 3);
        $area_cat2[$value]  = $cat2[$key];
        $area_cat3[$value]  = $cat3[$key];
        $area_brand[$value] = array_slice($brand_list, $key*4, 4);
        $area_hotads[$value] = array_slice($hot_ads, $key*6, 6);
    }
    $smarty->assign('area', $area);
    $smarty->assign('area_ads', $area_ads);
    $smarty->assign('area_cat2', $area_cat2);
    $smarty->assign('area_cat3', $area_cat3);
    $smarty->assign('area_brand', $area_brand);
    $smarty->assign('area_hotads', $area_hotads);

    $smarty->assign('hot_goods', get_hot_goods()); // 首页更多单品区
    $smarty->display('index/index.dwt');
    


/*------------------------------------------------------ */
//-- 当前模块特有函数库
/*------------------------------------------------------ */

/**
 * 热销小图产品区
 *
 * @access  private
 * @return  array
 */
function get_hot_goods()
{
    $sql = "SELECT g.goods_id, g.goods_name, g.shop_price, g.promote_price, g.goods_thumb , g.goods_img, g.is_hot, g.market_price
        FROM {$GLOBALS['db']->table('goods')} AS g
        WHERE g.is_on_sale = 1 AND g.is_alone_sale = 1 AND g.is_delete = 0 AND g.is_hot = 1
        ORDER BY RAND()
        LIMIT 9";
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    $idx = 0;
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$idx]['goods_id']         = $row['goods_id'];
        $arr[$idx]['goods_name']       = $row['goods_name'];
        $arr[$idx]['shop_price']       = $row['shop_price'];
        $arr[$idx]['market_price']     = $row['market_price'] > 0 ? $row['market_price'] : sprintf('%.2f', $row['shop_price'] * 1.5);
        $arr[$idx]['promote_price']    = $row['promote_price'];
        $arr[$idx]['goods_thumb']      = getUrl('goods_thumb', $row['goods_thumb']);
        $arr[$idx]['goods_img']        = getUrl('goods_img', $row['goods_img']);
        $arr[$idx]['url']              = getUrl('goods', $row['goods_id']);
        $idx ++;
    }
    return $arr;
}

/* 首页获取品牌列表 */
function index_brand_list()
{
    $sql = "SELECT * FROM {$GLOBALS['db']->table('brand')} WHERE sort_order between 0 and 48 ORDER BY RAND() LIMIT 24 ";
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    $idx = 0;
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$idx]         = $row;
        $arr[$idx]['brand_logo']        = getUrl('brand_logo', $row['brand_logo']);
        $idx ++;
    }
    return $arr;
    return $GLOBALS['db']->getAll($sql);
}

/* 首页获取发现列表 */
function index_tvd_list() {
    $sql = "SELECT tvd.list_img, tvd.tvd_id, tvd.title FROM {$GLOBALS['db']->table('tvd')} tvd
        WHERE is_open = 1
        ORDER BY sort DESC, add_time DESC LIMIT 8 ";
    $res = $GLOBALS['db']->query($sql);

    $arr = array();
    $idx = 0;
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $arr[$idx]         = $row;
        $arr[$idx]['url']        = getUrl('tvd', $row['tvd_id']);
        $idx ++;
    }
    return $arr;
}

/* 获取专区二级分类数据 */
function index_area_cat2() {
    $catArr = array('298', '303', '486', '542', '438', '211');
    foreach ($catArr as $key => $value) {
        $sql = "SELECT cat_id, cat_name FROM {$GLOBALS['db']->table('category')} WHERE parent_id = '$value' AND cat_id NOT IN (597,617,307,479)";
        $arr[] = $GLOBALS['db']->getAll($sql);
    }
    return $arr;
}

/* 获取专区三级热门分类数据 */
function index_area_cat3() {
    $arr[] = array(
        '393' => '水粉颜料',
        '397' => '丙烯水粉',
        '399' => '素描纸/本',
        '402' => '水彩纸/本',
        '411' => '扇形笔',
        '414' => '炭笔',
        '424' => '颜料盒',
        '432' => '笔袋',
    );
    $arr[] = array(
        '312' => '蜡笔/油画棒',
        '316' => '儿童水粉',
        '325' => '粘土/橡皮泥',
        '326' => '泥塑工具',
        '329' => '自动铅笔',
        '343' => '手工书籍',
        '347' => '彩绘笔',
        '351' => '书写用具',
    );
    $arr[] = array(
        '488' => '油画颜料',
        '534' => '画架/画板',
        '506' => '纯棉画框',
        '530' => '油画刀',
        '520' => '调色油',
        '523' => '上光油',
        '495' => '狼毫',
        '497' => '猪鬃',
    );
    $arr[] = array(
        '565' => '漫画网点纸',
        '562' => '原稿纸',
        '544' => '自动铅笔',
        '563' => '水彩本',
        '557' => '固体水彩',
        '560' => '漫画墨水',
        '571' => '手账贴纸',
        '572' => '橡皮擦',
    );
    $arr[] = array(
        '441' => '国画颜料',
        '443' => '墨汁/墨条',
        '446' => '生宣',
        '445' => '熟宣',
        '469' => '砚台',
        '474' => '笔洗',
        '460' => '狼毫',
        '462' => '兼毫',
    );
    $arr[] = array(
        '212' => '素描',
        '213' => '色彩',
        '214' => '速写',
        '222' => '考试',
        '215' => '静物照片',
        '216' => '头像照片',
        '217' => '速写照片',
        '218' => '风景照片',
    );
    return $arr;
}

/**
 * 根据商品ID获取二级分类下的商品信息
 * @param  str    $area    标识
 * @param  int    $cid     分类ID
 * @return arr      分类商品
 */
function indexAreaGoods($area = '', $cid = '') {
    /* 美术高考 */
    $arr['arts_exam'][398] = array(542,1099,1697,701,902,1622);
    $arr['arts_exam'][299] = array(361,1357,1869,855,354,371);
    $arr['arts_exam'][406] = array(1476,474,1089,1771,684,415);
    $arr['arts_exam'][419] = array(628,500,421,2883,684,415);
    $arr['arts_exam'][601] = array(1176,2479,2884,1212,1214,1210);
    /* 儿童少儿 */
    $arr['children'][304] = array(1791,1256,2885,2886,2097,2887);
    $arr['children'][305] = array(2888,2053,2889,2890,2891,2281);
    $arr['children'][306] = array(2042,1864,2505,2892,2258,2287);
    $arr['children'][308] = array(2333,2064,1844,2049,2735,2737);
    $arr['children'][310] = array(2480,1929,1930,2564,2525,2890);
    /* 油画 */
    $arr['acrylic_painting'][487] = array(2895,346,353,852,371,2896);
    $arr['acrylic_painting'][491] = array(461,2897,466,1361,2898,2899);
    $arr['acrylic_painting'][503] = array(652,2900,2901,2902,2903,2904);
    $arr['acrylic_painting'][513] = array(2905,2906,2907,545,2908,2909);
    $arr['acrylic_painting'][519] = array(492,2910,1683,2911,2912,2913);
    $arr['acrylic_painting'][529] = array(1014,502,835,2914,2915,2916);
    /* 设计动漫 */
    $arr['design'][543] = array(2507,2474,2564,2709,2656,1346);
    $arr['design'][555] = array(2478,1804,2476,352,2547,345);
    $arr['design'][561] = array(2893,701,1504,2596,1776,1775);
    $arr['design'][570] = array(2894,628,2622,2193,2614,2962);
    /* 国画书法 */
    $arr['rock_painting'][439] = array(2917,1482,2918,2485,2919,1841);
    $arr['rock_painting'][444] = array(2845,2921,2761,2759,2922,2923);
    $arr['rock_painting'][458] = array(2924,2925,2926,2527,2927,2534);
    $arr['rock_painting'][468] = array(2928,3038,3027,2778,3139,2862);

    /* 重组商品ID */
    if (!empty($area) && !empty($arr[$area])) {
        if (!empty($cid) &&  !empty($arr[$area][$cid])) {
            $arr = $arr[$area][$cid];
        } else {
            $arr = current($arr[$area]);
        }
        $strArr = implode(',', $arr);
    } else {
        return false;
    }

    /* 获取商品信息 */
    $sql = "SELECT goods_id, goods_name, shop_price, market_price, goods_thumb FROM {$GLOBALS['db']->table('goods')} WHERE goods_id IN ({$strArr}) ";
    $res = $GLOBALS['db']->query($sql);
    while ($row = $GLOBALS['db']->fetchRow($res))
    {
        $goods[$row['goods_id']]['goods_id']         = $row['goods_id'];
        $goods[$row['goods_id']]['goods_name']       = $row['goods_name'];
        $goods[$row['goods_id']]['shop_price']       = $row['shop_price'];
        $goods[$row['goods_id']]['market_price']     = $row['market_price'] > 0 ? $row['market_price'] : sprintf('%.2f', $row['shop_price'] * 1.5);
        $goods[$row['goods_id']]['goods_thumb']      = getUrl('goods_thumb', $row['goods_thumb']);
        $goods[$row['goods_id']]['url']              = getUrl('goods', array('id' => $row['goods_id']));
    }

    /* 重组数组 */
    foreach ($arr as $key => $value) {
        if (!empty($goods[$value])) {
            $arrGoods[$value] = $goods[$value];
        } else {
            $arrGoods[$value] = '';
        }
    }
    return $arrGoods;
}

/**
 * 根据标识获取专区内热门广告信息
 * @param  str    $area    标识
 * @return arr      分类商品
 */
function indexAreaHotAds($area = '') {

    /* 重组商品ID */
    $arrArea = array('arts_exam', 'children', 'acrylic_painting', 'design', 'rock_painting', );

    if (!empty($area) && in_array($area, $arrArea)) {
        $key = array_search($area, $arrArea);
        $limit = " LIMIT " . $key*6 . ", 6";
    } else {
        $limit = "";
    }

    $time = time();
    /* 获取商品信息 */
    $sql = "SELECT * FROM {$GLOBALS['db']->table('ad')} WHERE position_id = '50' AND start_time <= $time AND end_time >= $time AND enabled = 1 ORDER BY sort ASC, ad_name ASC $limit";
    $data = $GLOBALS['db']->getAll($sql);
    if (!empty($data)) {
        foreach ($data as $key => $value) {
            $data[$key]['url'] = getUrl('ad', $value);
            $data[$key]['img_url'] = getUrl('ad_img', $value);
        }
    }
    return $data;
}